package cn.com.nes.site.service.iot.impl;

import cn.com.nes.common.exception.IllegalBusinessException;
import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.com.nes.mybatis.agent.ope.entity.IsWxsb;
import cn.com.nes.mybatis.agent.ope.mapper.IsWxsbMapper;
import cn.com.nes.site.service.iot.IotDtuService;
import cn.com.nes.site.service.iot.IsVersionService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class IotDtuServiceImpl extends ServiceImpl<IsWxsbMapper, IsWxsb> implements IotDtuService {

    @Resource
    private IsVersionService isVersionService;

    @Override
    public IsWxsb getDtu(String dtuId) throws Exception {
        if (StrUtil.isEmpty(dtuId)) {
            throw new IllegalBusinessException("dtuId must not empty");
        }
        return this.baseMapper.getDtu(dtuId);
    }

    @Override
    public void updateDtuVersion(List<String> dtuIdList) {
        if (CollectionUtil.isEmpty(dtuIdList)) {
            throw new MissBusinessParameterException("Dtu号列表");
        }

        // 取当前时间戳做版本号
        long version = System.currentTimeMillis();
        log.info("更新Dtu版本号和总版本号,版本号:" + version);
        // 更新DTU版本号
        this.baseMapper.updateDtuVersion(dtuIdList, version);
        // 更新总版本号
        isVersionService.updateVersion(version);
    }

}
